package com.ujoku.controller;

import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.platform.web.rest.exception.InvalidRequestException;
import com.labillusion.core.util.UUIDUtils;
import com.ujoku.context.ActionContext;
import com.ujoku.domain.Address;
import com.ujoku.domain.Cart;
import com.ujoku.domain.Member;
import com.ujoku.interceptor.LoginRequired;
import com.ujoku.request.AddressForm;
import com.ujoku.service.AddressService;
import com.ujoku.service.RegionService;
import com.ujoku.view.domain.AddressView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Greg on 14-10-25.
 */
@RestController
public class AddressController extends RESTController {

    @Autowired
    public AddressService service;
    @Autowired
    public RegionService regionService;

    @Autowired
    private ActionContext actionContext;

    @RequestMapping(value="/address/create", method = RequestMethod.POST)
    @ResponseBody
    @LoginRequired
    public Address Create(@Valid @RequestBody AddressForm form){

        Member member = actionContext.getMember();
        List<Address> addressList = service.selectByMemberId(member.getUser_id());
        if(addressList.size() > 4)
            throw new InvalidRequestException("地址簿里最多只能有5条记录.");

        Address address = new Address();
        BeanUtils.copyProperties(form,address);
        address.setId(UUIDUtils.random());
        address.setMemberId(member.getUser_id());
        if(addressList.size() == 0){
            address.setPrimary(true);
        }

        service.insert(address);
        return address;
    }

    @RequestMapping(value="/address", method = RequestMethod.GET)
    @ResponseBody
    @LoginRequired
    public AddressView getAddressByUserId(){

        Member member = actionContext.getMember();

        return new AddressView(service.selectByMemberId(member.getUser_id()));
    }

    @RequestMapping(value="/address/primary/{id}", method = RequestMethod.PUT)
    @ResponseBody
    @LoginRequired
    public Member updatePrimary(@PathVariable String id){

        Member member = actionContext.getMember();
        List<Address> addressList = service.selectByMemberId(member.getUser_id());
        for(int i = 0;i < addressList.size(); i ++){
            Address address = addressList.get(i);
            address.setPrimary(false);
            service.update(address);
        }
        Address address = service.selectById(id,member.getUser_id());
        address.setPrimary(true);
        service.update(address);

        member.setPrimaryAddress(address);
        actionContext.setMember(member);

        return member;
    }

    @RequestMapping(value="/address/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    @LoginRequired
    public String delete(@PathVariable String id){
        service.deleteById(id);

        Member member = actionContext.getMember();
        List<Address> addressList = service.selectByMemberId(member.getUser_id());

        //如果没有primary address, 更新一个默认地址
        if(addressList.size() > 0){
            boolean hasPrimary = addressList.stream().anyMatch(a->a.getPrimary());
            if(!hasPrimary){
                Address address = addressList.get(0);
                address.setPrimary(true);
                service.update(address);
            }
        }

        Cart cart = actionContext.getCart();
        if(cart != null && cart.getShippingAddress() != null){
            if(id.equalsIgnoreCase(cart.getShippingAddress().getId())){
                cart.setShippingAddress(null);
                actionContext.setCart(cart);
            }
        }


        return "Successfully";
    }
}
